package com.tjp.eight.ui.fragment.tabhome;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.AnimationUtils;
import com.tjp.eight.utils.DipPxSpUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.views.lazyviewpager.view.indicator.IndicatorViewPager;
import com.tjp.eight.views.lazyviewpager.view.indicator.ScrollIndicatorView;
import com.tjp.eight.views.lazyviewpager.view.indicator.slidebar.ColorBar;
import com.tjp.eight.views.lazyviewpager.view.indicator.slidebar.DrawableBar;
import com.tjp.eight.views.lazyviewpager.view.indicator.transition.OnTransitionTextListener;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/13.
 */

public class FragmentHome extends BaseFragment<HomePresenter> implements HomeView {
    private IndicatorViewPager indicatorViewPager;
    private ScrollIndicatorView sv_home;
    private ViewPager viewPager;
    private HomeModel homeModels;//
    private ArrayList<HomeModel.HomeClass> homeClasses;
    private LinearLayout ll_content;
    private RelativeLayout rl_loading;
    private ImageView iv_no_data;
    private ProgressBar progressBar;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.frag_home_layout, container, false);
        }

        initViews();

        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData();
    }

    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter(this);
    }

    @Override
    public void loadData(HomeModel homeModel) {
        if (homeModel == null) {
            rl_loading.setVisibility(View.VISIBLE);
            iv_no_data.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);
            return;
        }
        AnimationUtils.alphaViewGone(rl_loading);
        AnimationUtils.alphaViewGone(progressBar);
        if (IsSuccessUtils.isSuccess(homeModel.result)) {
            this.homeModels = homeModel;
            homeClasses.clear();
            for (HomeModel.HomeClass homeClass : homeModel.data) {
                homeClasses.add(homeClass);
            }
            indicatorViewPager.setAdapter(new HomeTypesAdapter(getChildFragmentManager()));
        } else {
            Toast.makeText(getContext(), "" + homeModel.error, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void loadFails(String msg) {
        rl_loading.setVisibility(View.VISIBLE);
        iv_no_data.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showToast(String msg) {

    }

    void initViews() {
        iv_no_data = (ImageView) contentView.findViewById(R.id.iv_no_data);
        iv_no_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                presenter.loadData();
            }
        });
        progressBar = (ProgressBar) contentView.findViewById(R.id.pb_loading);
        rl_loading = (RelativeLayout) contentView.findViewById(R.id.rl_loading);
        ll_content = (LinearLayout) contentView.findViewById(R.id.ll_content);
        sv_home = (ScrollIndicatorView) contentView.findViewById(R.id.sv_home);
//        sv_home.setScrollBar(new ColorBar(getActivity(), 0xff1abdb8, 5));
        Drawable drawable = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            drawable = getActivity().getResources().getDrawable(R.drawable.tab_barb, getActivity().getResources().newTheme());
        }else{
            drawable = getActivity().getResources().getDrawable(R.drawable.tab_barb);
        }
        sv_home.setScrollBar(new DrawableBar(getActivity(),drawable));
        viewPager = (ViewPager) contentView.findViewById(R.id.vp_home);
        viewPager.setOffscreenPageLimit(1);
        // 设置滚动监听
        int selectColorId = R.color.white;
        int unSelectColorId = R.color.white;
        sv_home.setOnTransitionListener(new OnTransitionTextListener().
                setColorId(getActivity(), selectColorId, unSelectColorId)
                .setSize(15, 14));
        indicatorViewPager = new IndicatorViewPager(sv_home, viewPager);
        homeClasses = new ArrayList<>();
    }

    class HomeTypesAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {
        public HomeTypesAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return homeClasses != null ? homeClasses.size() : 0;
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.home_types, container, false);
            }
            TextView textView = (TextView) convertView;
            if (homeClasses != null) {
                textView.setText(homeClasses.get(position % homeClasses.size()).name);
            }
            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            FragmentHomeType homeType = new FragmentHomeType();
            if ("互动".equals(homeClasses.get(position % homeClasses.size()).name)) {
                homeType.setType(1);
            } else {
                homeType.setType(0);
            }
            homeType.setCaId(homeClasses.get(position % homeClasses.size()).caid);
            return homeType;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }
    }
}
